package com.mapaoer.user.controller;

import com.mapaoer.user.entity.SkillTag;
import com.mapaower.user.service.SkillTagService;
import com.mapaoer.user.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 技能标签控制器
 * 处理技能标签相关的API请求
 */
@RestController
@RequestMapping("/api/skill")
public class SkillTagController {

    @Autowired
    private SkillTagService skillTagService;

    /**
     * 创建技能标签（管理员）
     * @param name 技能名称
     * @param category 技能类别
     * @param color 技能颜色
     * @param isHot 是否热门
     * @return 创建的技能标签
     */
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> createSkillTag(
            @RequestParam String name,
            @RequestParam String category,
            @RequestParam(required = false) String color,
            @RequestParam(required = false) Boolean isHot) {
        SkillTag skillTag = skillTagService.createSkillTag(name, category, color, isHot);
        if (skillTag != null) {
            return ResponseEntity.ok(ResponseResult.success(skillTag));
        } else {
            return ResponseEntity.badRequest().body(ResponseResult.error("技能标签已存在"));
        }
    }

    /**
     * 获取所有技能标签
     * @return 技能标签列表
     */
    @GetMapping
    @PreAuthorize("hasAnyRole('DEVELOPER', 'ENTERPRISE', 'ADMIN')")
    public ResponseEntity<ResponseResult<?>> getAllSkillTags() {
        List<SkillTag> skillTags = skillTagService.getAllSkillTags();
        return ResponseEntity.ok(ResponseResult.success(skillTags));
    }

    /**
     * 根据ID获取技能标签
     * @param id 技能标签ID
     * @return 技能标签
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('DEVELOPER', 'ENTERPRISE', 'ADMIN')")
    public ResponseEntity<ResponseResult<?>> getSkillTagById(@PathVariable Long id) {
        SkillTag skillTag = skillTagService.getSkillTagById(id);
        if (skillTag != null) {
            return ResponseEntity.ok(ResponseResult.success(skillTag));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 根据类别获取技能标签列表
     * @param category 技能类别
     * @return 技能标签列表
     */
    @GetMapping("/category/{category}")
    @PreAuthorize("hasAnyRole('DEVELOPER', 'ENTERPRISE', 'ADMIN')")
    public ResponseEntity<ResponseResult<?>> getSkillTagsByCategory(@PathVariable String category) {
        List<SkillTag> skillTags = skillTagService.getSkillTagsByCategory(category);
        return ResponseEntity.ok(ResponseResult.success(skillTags));
    }

    /**
     * 获取热门技能标签
     * @return 热门技能标签列表
     */
    @GetMapping("/hot")
    @PreAuthorize("hasAnyRole('DEVELOPER', 'ENTERPRISE', 'ADMIN')")
    public ResponseEntity<ResponseResult<?>> getHotSkillTags() {
        List<SkillTag> skillTags = skillTagService.getHotSkillTags();
        return ResponseEntity.ok(ResponseResult.success(skillTags));
    }

    /**
     * 更新技能标签（管理员）
     * @param id 技能标签ID
     * @param name 技能名称
     * @param category 技能类别
     * @param color 技能颜色
     * @param isHot 是否热门
     * @return 更新后的技能标签
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> updateSkillTag(
            @PathVariable Long id,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String color,
            @RequestParam(required = false) Boolean isHot) {
        SkillTag skillTag = skillTagService.updateSkillTag(id, name, category, color, isHot);
        if (skillTag != null) {
            return ResponseEntity.ok(ResponseResult.success(skillTag));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 删除技能标签（管理员）
     * @param id 技能标签ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> deleteSkillTag(@PathVariable Long id) {
        boolean result = skillTagService.deleteSkillTag(id);
        if (result) {
            return ResponseEntity.ok(ResponseResult.success("技能标签删除成功"));
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}